//
//  Message.swift
//  app_rn_frontend
//
//  Created by 鱼涛 on 2017/12/14.
//  Copyright © 2017年 Facebook. All rights reserved.
//

import Foundation
import WCDBSwift


enum ChatType: Int {
    case p2p = 1
    case group = 2
    case chatRoom = 3
    case service_platform = 4
}

extension ChatType: ColumnCodable,LiteralValueConvertible {
    init?(with value: FundamentalValue) {
        let v = Int(value.int32Value)
        self = ChatType(rawValue: v) ?? ChatType.p2p
    }
    
    func archivedValue() -> FundamentalValue {
        return FundamentalValue(self.rawValue)
    }
    
    static var columnType: ColumnType {
        return .integer64
    }
}

enum MessageStatus: Int {
    case pending = 1
    case success = 2
    case error = 3
}

extension MessageStatus: ColumnCodable,LiteralValueConvertible {
    init?(with value: FundamentalValue) {
        let v = Int(value.int32Value)
        self = MessageStatus(rawValue: v) ?? MessageStatus.success
    }
    
    func archivedValue() -> FundamentalValue {
        return FundamentalValue(self.rawValue)
    }
    
    static var columnType: ColumnType {
        return .integer64
    }
}

enum MessageType: Int {
    case unknown = -1
    case time = 0
    case text = 1
    case img = 2
    case audio = 3
    case video = 4
    case notice = 5
    case file = 6
    case danmu = 7
    case sticker = 8  //贴图 或 gif表情,content字段内容为:  {"name":"大笑","category":"ajmd","sticker":"ajmd001.png"}  //仅支持jpg png gif, 如果category 为空字符串,表示用户从外部引入的自定义表情
}

enum NoticeType: Int {
    case unknown = -1                   //未知
    case modifyGroupName = 0            //修改群名称  {type:0,name:"修改后的群昵称"}
    case modifyGroupNickName = 1        // 修改个人在此群的昵称 {type:1,nickName:"修改后的昵称"}
    case modifyGroupAnnouncement = 2    //发布群公告 {type:2,announcement:"修改后的群公告"}
    case muteAll = 3                    //全体禁言 {type:3}
    case mute = 4                       //部分禁言 {type:4,userIds:[]}
    case unMuteAll = 5                  //全体解禁 {type:5}
    case unMute = 6                     //部分解禁 {type:6,userIds:[]}
    case addMember = 7                  //添加群成员{type:7,userIds:[]}
    case delMember = 8                  //删除群成员{type:8,userIds:[]}
    case changeMemberRole = 9           //更改群成员角色{type:9,userId:3000001,role:1}
    case memberQuit = 10                //成员主动退群{type:10}
    case changeMaster = 11              //群主变更{type:11,userId:300001}
}


final class Message: TableCodable {
    static let table = "message"
    
    var id: Int = 0
    var syncKey: Int? = nil
    var sessionId: Int = 0
    var sessionServerId: Int = 0
    var chatType: ChatType = .p2p
    var userId: Int = 0
    var messageType: MessageType {
        get {
            return MessageType.init(rawValue: self.msgType) ?? MessageType.unknown
        }
    }
    
    var msgType: Int = -1
    var content: String? = nil
    var me: Bool = false
    var createTime: Date = Date()
    var createdDatetime: String = ""
    var ts: Date = Date()
    var deleted: Bool = false
    var unread: Bool = true
    var audioRead: Bool = true
    var status:MessageStatus = .success
    
    var msgText:String {
        switch (messageType) {
        case .text:
            return content ?? ""
        case .audio:
            return "[语音]"
        case .img:
            return "[图片]"
        case .video:
            return "[视频]"
        case .file:
            return "[文件]"
        case .notice:
            return "[通知消息]"
        default:
            return "[未知消息类型,请升级App]"
        }
    }
    
    enum CodingKeys: String, CodingTableKey {
        typealias Root = Message
        case id
        case syncKey = "sync_key"
        case sessionId = "session_id"
        case sessionServerId = "session_server_id"
        case chatType = "chat_type"
        case userId = "user_id"
        case msgType = "message_type"
        case content
        case me
        case createTime = "create_time"
        case createdDatetime = "created_datetime"
        case ts
        case deleted
        case unread
        case audioRead
        case status
        
        static let objectRelationalMapping = TableBinding(CodingKeys.self) {
            BindColumnConstraint(.id, isPrimary: true, isAutoIncrement: true)
            BindIndex(sessionServerId.asIndex(), namedWith: "_sessionIdIndex")
            BindIndex(createTime.asIndex(), namedWith: "_createTimeIndex")
        }
    }
    
    var isAutoIncrement: Bool = true
    var lastInsertedRowID: Int64 = 0
}
